//
//  YTFollowAuthorController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/7/26.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTFollowAuthorController.h"
#import "YTFollowAutherCell.h"
#import "YTFollowAuthorHeaderView.h"
#import "YTAuthorListController.h"
#import "YTNoDataView.h"
#import "YTNoDataCell.h"

NSString *followAuthorCellID = @"followAuthorCellID";
NSString *noDataCellID = @"noDataCellID";
@interface YTFollowAuthorController ()

@property(nonatomic ,strong)YTFollowAuthorModel *model;


@property(nonatomic ,assign)BOOL isNoData;

@end

@implementation YTFollowAuthorController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];

}

// 设置界面
- (void)setupUI
{
    // setup headerView
    YTFollowAuthorHeaderView *view = [YTFollowAuthorHeaderView headerView];
    
    view.viewClickBlock = ^{
        
        YTAuthorListController *authorListVC = [[YTAuthorListController alloc] init];
        authorListVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:authorListVC animated:true];
    };
    
    self.tableView.tableHeaderView = view;
    self.tableView.sectionHeaderHeight = 64;
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    // setup nav
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"倛袨1"]
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self action:@selector(backButtonClick)];
    [self.navigationItem.leftBarButtonItem setTintColor:kMainColor];
    self.navigationItem.title = @"我的关注";
    
    
    // setup refresh
    YTRefreshHeader *header = [YTRefreshHeader headerWithRefreshingBlock:^{
        [self loadDataWithPullup:NO];
    }];
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadDataWithPullup:YES];
    }];
    [footer setTitle:@"没有更多了哦~" forState:MJRefreshStateNoMoreData];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    self.tableView.mj_header = header;
    self.tableView.mj_footer = footer;
    
    self.tableView.backgroundColor = [UIColor colorWithHex:@"f1f2f2"];
    // setup table view
    [self.tableView registerNib:[UINib nibWithNibName:@"YTFollowAutherCell" bundle:nil] forCellReuseIdentifier:followAuthorCellID];
    [self.tableView registerNib:[UINib nibWithNibName:@"YTNoDataCell" bundle:nil] forCellReuseIdentifier:noDataCellID];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}




#pragma mark； - 加载数据
- (void)loadDataWithPullup:(BOOL)pullup
{
    int page = self.model ? self.model.page.intValue : 1;
    
    if (pullup) {
        page++;
        if (self.model.list.count >= self.model.total.intValue) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
    }else{
        page = 1;
    }
    
    [YTNetworkManager.shareManager requestFollowListWithIsAuthorList:NO paged:page
                                completion:^(YTFollowAuthorModel *model, BOOL isSucc, NSString *error)
     {
         if (!isSucc) {
             [self.view showTipWithTipStr:error];
             pullup ? [self.tableView.mj_footer endRefreshing] : [self.tableView.mj_header endRefreshing];
             return ;
         }
         
         if (pullup) {
             self.model.list = [self.model.list arrayByAddingObjectsFromArray:model.list];
             [self.tableView.mj_footer endRefreshing];
         }else{
             self.model = model;
             self.isNoData = !(model.list.count > 0);
             [self.tableView.mj_header endRefreshing];
         }
         
         error ? nil : [self.tableView reloadData];
    }];
}

#pragma mark: - table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.isNoData ? 1 : self.model.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.isNoData ? noDataCellID : followAuthorCellID
                                                                forIndexPath:indexPath];
    if (!self.isNoData) {
        [(YTFollowAutherCell *)cell setIsAuthorList:NO];
        [(YTFollowAutherCell *)cell setModel:self.model.list[indexPath.row]];
        [(YTFollowAutherCell *)cell setToFollowAuthorBlock:^(NSInteger idx) {
            [self loadDataWithPullup:NO];

        }];
        
        
        [(YTFollowAutherCell *)cell setAuthorIconClickBlock:^(NSString *authorID) {
            YTAuthorInfoController *infoVC = [[YTAuthorInfoController alloc] init];
            infoVC.user_id = authorID;
            [self.navigationController pushViewController:infoVC animated:true];
        }];
 
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.tag = indexPath.row;
    return cell;
}
#pragma mark: - table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.isNoData ? kScreenHeight - 64 - kNavbarHeight : 74;
}

#pragma mark: - 返回按钮 监听
- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:true];
}


@end
